package com.story.api.common.aop;

import com.story.core.aop.BaseControllerExceptionHandler;
import com.story.core.base.tips.ErrorTip;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.xml.bind.ValidationException;

/**
 * 全局的的异常拦截器（拦截所有的控制器）（带有@RequestMapping注解的方法上都会拦截）
 * @author yangbo
 * @date 2017-12-05 10:16
 */
@ControllerAdvice
public class GlobalExceptionHandler extends BaseControllerExceptionHandler {

    private Logger log = LoggerFactory.getLogger(this.getClass());


    /**
     * 404
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseBody
    public ErrorTip handleHttpNotFoundException(NoHandlerFoundException e) {
        log.error("路径错误",e);
        return new ErrorTip(HttpStatus.NOT_FOUND.value(),"no handler found");
    }

    /**
     * 请求参数错误
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public ErrorTip handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        log.error("参数解析错误",e);
        return new ErrorTip(HttpStatus.BAD_REQUEST.value(),"parameter error");
    }

    /**
     * 请求方法不支持
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public ErrorTip handlerHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        log.error("不支持当前请求方法",e);
        return new ErrorTip(HttpStatus.METHOD_NOT_ALLOWED.value(),"request method not supported");
    }

    /**
     * 不支持当前媒体类型错误
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    @ResponseBody
    public ErrorTip handlerHttpMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
        log.error("不支持当前媒体类型",e);
        return new ErrorTip(HttpStatus.UNSUPPORTED_MEDIA_TYPE.value(),"media type not supported");
    }

    /**
     * 参数未呈现
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MissingServletRequestPartException.class)
    @ResponseBody
    public ErrorTip handlerMissingServletRequestPartException(MissingServletRequestPartException e) {
        log.error("未传参数",e);
        return new ErrorTip(HttpStatus.BAD_REQUEST.value(),"Required request part is not present");
    }

    /**
     * 参数校验错误
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(ValidationException.class)
    public ErrorTip handlerValidateException(ValidationException e) {
        log.error("参数校验错误",e);
        return new ErrorTip(HttpStatus.BAD_REQUEST.value(),"validation parameter error");
    }


    /**
     * 参数校验错误
     * @param e
     * @return
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ErrorTip handlerMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error("参数校验错误",e);
        return new ErrorTip(HttpStatus.BAD_REQUEST.value(),"validation parameter error");
    }

}
